﻿using Electronic_work_order.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Electronic_work_order.Interfacess.OA_Users
{
    //用户所有查询
    public class Sql_Users : Interface_Usrs
    {
        private readonly AppDbContext context;

        public Sql_Users(AppDbContext context)
        {
            this.context = context;
        }
        //添加职员
        public int AddUsers(Users users)
        {
           context.Users.Add(users);
          return  context.SaveChanges();
        }
        //修改职员信息;
        public int UpdateUsers(Users users)
        {
            var u = context.Users.Attach(users);
            u.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return context.SaveChanges();


        }
        //查询状态
        public IEnumerable<UserState> GetStates()
        {
           return context.UserState.ToList();
        }

        //查询人员并显示职位
        public IEnumerable<Users> GetUsers()
        {
           return context.Users.Include(a => a.Position).Include(b=>b.UserState).Include(c=>c.UserRole).ToList();
        }
        //根据名字模糊查询
        public IEnumerable<Users> GetUsers_MoHu(string UsersName)
        {
           return context.Users.Include(a => a.Position).Include(a=>a.UserState).Include(b=>b.UserRole).Where(a => a.UsersName.Contains(UsersName));
        }
        //删除人员（修改其状态）
        public int UpdateState(int UsersID)
        {
          Users users=  context.Users.Where(a => a.UsersID == UsersID).FirstOrDefault();
            users.StateID = 2;
            return context.SaveChanges();
        }
        //查询所有部门
        public IEnumerable<Users> GetOrganizations()
        {
            // return context.Users.Include(a=>a.Position).Where(a =>a.Position.Grade== 2).ToList();
            return context.Users.Include(a => a.Position).ToList();
        }
        //查询职位
        public IEnumerable<Position> GetPositions()
        {
            return context.Position.ToList();
        }
    }
}
